﻿using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Collections.Generic;
using System.Collections;
using Wsitech.Model;
using Wsitech.Utility;
/// <summary>
///BackendPageBase 的摘要说明
/// </summary>
public class BackendPageBase : System.Web.UI.Page
{
    public BackendPageBase()
    {
        //
        //TODO: 在此处添加构造函数逻辑
        //
    }

    protected string BACKEND_LOGOUT = "/Backend/Logout.aspx";
    protected string BACKEND_LOGIN = "/Backend/Admin_Login.aspx";

    protected string BACKEND_INDEX = GlobalConst.BACKEND_INDEX;

    protected void Page_PreInit(object sender, EventArgs e)
    {
        try
        {
            if (!IsVerified)
            {
                //Response.Redirect(URL_LOGIN);

                //frameset
                string script = "top.location='" + BACKEND_LOGIN + "'";
                RunScript("", "", script);
            }

            //this.Theme = "BackendGreen";
        }
        catch (Exception)
        {

            throw;
        }
    }

    public bool IsVerified
    {
        get
        {
            //return true;
            return VerifiedUser != null;
        }

    }

    public AdminUser VerifiedUser
    {
        get
        {
            if (Session["BackendUser"] != null)
            {
                return Session["BackendUser"] as AdminUser;
            }
            else
            {
                return null;
            }
        }
    }

    #region 获取枚举类型的键值对

    /// <summary>
    /// 获取枚举类型的键值对
    /// </summary>
    /// <typeparam name="T"></typeparam>
    /// <returns></returns>
    public IList<ReadEnum> GetEnumList<T>()
    {
        Type type = typeof(T);
        IList<ReadEnum> list = new List<ReadEnum>();
        foreach (int value in Enum.GetValues(type))
        {
            string name = Enum.GetName(type, value);
            string description = GetEnumDesc(type, value);
            list.Add(new ReadEnum(name, value, description));
        }
        return list;
    }

    /// <summary>
    /// 获得Enum类型description
    /// 创建人：Porschev
    /// 创建时间：2011-7-19
    /// </summary>
    /// <param name="enumType">枚举的类型</param>
    /// <param name="val">枚举值</param>
    /// <returns>string</returns>
    public static string GetEnumDesc(Type enumType, object val)
    {
        string enumvalue = System.Enum.GetName(enumType, val);
        if (string.IsNullOrEmpty(enumvalue))
        {
            return "";
        }
        System.Reflection.FieldInfo finfo = enumType.GetField(enumvalue);
        object[] enumAttr = finfo.GetCustomAttributes(typeof(System.ComponentModel.DescriptionAttribute), true);
        if (enumAttr.Length > 0)
        {
            System.ComponentModel.DescriptionAttribute desc = enumAttr[0] as System.ComponentModel.DescriptionAttribute;
            if (desc != null)
            {
                return desc.Description;
            }
        }
        return enumvalue;
    }

    public class ReadEnum
    {
        public ReadEnum(string name, int value, string description)
        {
            this.name = name;
            this.value = value;
            this.description = description;
        }

        private string name;

        public string Name
        {
            get { return name; }
        }
        private int value;

        public int Value
        {
            get { return this.value; }
        }
        private string description;

        public string Description
        {
            get { return description; }
        }
    }
    #endregion

    #region 弹框相关

    /// <summary>
    /// JS弹框
    /// 创建人：Amos Wnn
    /// 创建时间：2011-10-14
    /// </summary>
    /// <param name="page"></param>
    /// <param name="context"></param>
    public void Alert(string alertContext)
    {
        this.RunScript(alertContext, null, null);
    }

    /// <summary>
    /// JS弹框并跳转
    /// 创建人：Amos Wnn
    /// 创建时间：2011-10-14
    /// </summary>
    /// <param name="page"></param>
    /// <param name="context"></param>
    public void AlertGo(string alertContext, string url)
    {
        this.RunScript(alertContext, url, null);
    }

    //关闭当前页并刷新父页面
    public void RefreshParent()
    {
        string js = @"parent.document.location.href=parent.document.location.href";
        this.RunScript("", "", js);
    }
    /// <summary>
    /// JS弹框并跳转追加其他js
    /// 创建人：Amos Wnn
    /// 创建时间：2011-10-14
    /// </summary>
    /// <param name="page"></param>
    /// <param name="context"></param>
    /// <param name="url"></param>
    public void RunScript(string alertContext, string url, string otherScripts)
    {
        System.Text.StringBuilder sb = new System.Text.StringBuilder();

        if (!string.IsNullOrEmpty(alertContext))
        {
            sb.AppendFormat("alert(\"{0}\");", alertContext);
        }

        if (!string.IsNullOrEmpty(url))
        {
            sb.AppendFormat("location.href=\"{0}\";", url);
        }

        if (!string.IsNullOrEmpty(otherScripts))
        {
            sb.AppendFormat("{0};", otherScripts);
        }

        Page.ClientScript.RegisterStartupScript(this.GetType(), "", sb.ToString(), true);
    }

    #endregion

    #region##保存为Excel
    /// <summary>
    /// 保存为Excel
    /// </summary>
    /// <param name="content"></param>
    public void GenerateReport(string content, string fileName)
    {
        Response.AddHeader("Content-Disposition", "attachment; filename=" + fileName);
        Response.ContentType = "application/ms-excel";// 指定返回的是一个不能被客户端读取的流，必须被下载
        Response.ContentEncoding = System.Text.Encoding.Default;
        Response.Write(content); // 把文件流发送到客户端
        Response.End();
    }
    #endregion


}
